package com.mh.domain;

import java.io.Serializable;

import java.lang.StringBuilder;

import java.util.Date;
import java.util.Set;

import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;

import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.*;

import javax.persistence.*;

import org.codehaus.jackson.annotate.JsonIgnore;
import org.codehaus.jackson.map.annotate.JsonDeserialize;
import org.codehaus.jackson.map.annotate.JsonSerialize;
import org.hibernate.validator.constraints.NotEmpty;

import com.mh.web.utils.DateJsonDeserializer;
import com.mh.web.utils.DateJsonSerializer;

/**
 * 
 * @ModelCoreReference [platform:/resource/demo-mh/.springDSL/com/mh/domain/Item.datatype]
 */

@Entity
@NamedQueries({
		@NamedQuery(name = "findAllItems", query = "select myItem from Item myItem"),
		@NamedQuery(name = "findItemByCost", query = "select myItem from Item myItem where myItem.cost = ?1"),
		@NamedQuery(name = "findItemByCreatedBy", query = "select myItem from Item myItem where myItem.createdBy = ?1"),
		@NamedQuery(name = "findItemByCreatedByContaining", query = "select myItem from Item myItem where myItem.createdBy like ?1"),
		@NamedQuery(name = "findItemByCreatedOn", query = "select myItem from Item myItem where myItem.createdOn = ?1"),
		@NamedQuery(name = "findItemById", query = "select myItem from Item myItem where myItem.id = ?1"),
		@NamedQuery(name = "findItemByModifiedBy", query = "select myItem from Item myItem where myItem.modifiedBy = ?1"),
		@NamedQuery(name = "findItemByModifiedByContaining", query = "select myItem from Item myItem where myItem.modifiedBy like ?1"),
		@NamedQuery(name = "findItemByModifiedOn", query = "select myItem from Item myItem where myItem.modifiedOn = ?1"),
		@NamedQuery(name = "findItemByPrimaryKey", query = "select myItem from Item myItem where myItem.id = ?1"),
		@NamedQuery(name = "findItemByQuantity", query = "select myItem from Item myItem where myItem.quantity = ?1") })
@Table(schema = "MHTEST", name = "ITEM")
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(namespace = "demo-mh/com/mh/domain", name = "Item")
public class Item implements Serializable {
	private static final long serialVersionUID = 1L;

	/**
	 */

	@Column(name = "ID", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@Id
	@XmlElement
	@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "seq_item")
	@SequenceGenerator(name = "seq_item", sequenceName = "seq_item")
	Integer id;

	/**
	 */

	@Column(name = "QUANTITY", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	Integer quantity;

	/**
	 */

	@Column(name = "COST", scale = 2, precision = 16, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	Double cost;

	/**
	 */

	@Column(name = "CREATED_BY", length = 30, nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	@NotEmpty
	String createdBy;

	/**
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "CREATED_ON", nullable = false)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	@NotNull
	Date createdOn;

	/**
	 */

	@Column(name = "MODIFIED_BY", length = 30)
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	String modifiedBy;

	/**
	 */
	@Temporal(TemporalType.TIMESTAMP)
	@Column(name = "MODIFIED_ON")
	@Basic(fetch = FetchType.EAGER)
	@XmlElement
	Date modifiedOn;

	/**
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumns({ @JoinColumn(name = "PRODUCT_ID", referencedColumnName = "ID", nullable = false) })
	@XmlTransient
	Product product;

	/**
	 */
	@ManyToOne(fetch = FetchType.EAGER)
	@JoinColumns({ @JoinColumn(name = "INVOICE_ID", referencedColumnName = "ID", nullable = false) })
	@XmlTransient
	Invoice invoice;

	/**
	 */
	public void setId(Integer id) {
		this.id = id;
	}

	/**
	 */

	public Integer getId() {
		return this.id;
	}

	/**
	 */
	public void setQuantity(Integer quantity) {
		this.quantity = quantity;
	}

	/**
	 */
	public Integer getQuantity() {
		return this.quantity;
	}

	/**
	 */
	public void setCost(Double cost) {
		this.cost = cost;
	}

	/**
	 */
	public Double getCost() {
		return this.cost;
	}

	/**
	 */
	public void setCreatedBy(String createdBy) {
		this.createdBy = createdBy;
	}

	/**
	 */
	public String getCreatedBy() {
		return this.createdBy;
	}

	/**
	 */
	@JsonDeserialize(using=DateJsonDeserializer.class)
	public void setCreatedOn(Date createdOn) {
		this.createdOn = createdOn;
	}

	/**
	 */
	@JsonSerialize(using=DateJsonSerializer.class)
	public Date getCreatedOn() {
		return this.createdOn;
	}

	/**
	 */
	public void setModifiedBy(String modifiedBy) {
		this.modifiedBy = modifiedBy;
	}

	/**
	 */
	public String getModifiedBy() {
		return this.modifiedBy;
	}

	/**
	 */
	@JsonDeserialize(using=DateJsonDeserializer.class)
	public void setModifiedOn(Date modifiedOn) {
		this.modifiedOn = modifiedOn;
	}

	/**
	 */
	@JsonSerialize(using=DateJsonSerializer.class)
	public Date getModifiedOn() {
		return this.modifiedOn;
	}
	
	
	

	/**
	 */
	@JsonIgnore
	public void setProduct(Product product) {
		this.product = product;
	}

	/**
	 */
	@JsonIgnore
	public Product getProduct() {
		return product;
	}

	/**
	 */
	@JsonIgnore
	public void setInvoice(Invoice invoice) {
		this.invoice = invoice;
	}

	/**
	 */
	@JsonIgnore
	public Invoice getInvoice() {
		return invoice;
	}

	/**
	 */
	public Item() {
	}

	/**
	 * Copies the contents of the specified bean into this bean.
	 *
	 */
	public void copy(Item that) {
		setId(that.getId());
		setQuantity(that.getQuantity());
		setCost(that.getCost());
		setCreatedBy(that.getCreatedBy());
		setCreatedOn(that.getCreatedOn());
		setModifiedBy(that.getModifiedBy());
		setModifiedOn(that.getModifiedOn());
		setProduct(that.getProduct());
		setInvoice(that.getInvoice());
	}
	


	private Integer invoiceId;
	private Integer productId;
	
	public String getProductName(){
		return this.product.getName();
	}
	
	@JsonIgnore
	public void setProductName(String productName){}
	
	public Integer getProductId(){
		return this.product.getId();
	}
	
	
	public void setProductId(Integer productId){ this.productId = productId;}
	
	public Integer getInvoiceId(){
		return this.invoice.getId();
	}
	
	public Integer getIntProductId(){return this.productId;}
	
	public Integer getIntInvoiceId(){return this.invoiceId;}
	public void setInvoiceId(Integer invoiceId){this.invoiceId = invoiceId;}
	
	public String getInvoiceDoc(){
		return this.invoice.getDocument();
	}
	
	@JsonIgnore
	public void setInvoiceDoc(String invoiceDoc){};

	/**
	 * Returns a textual representation of a bean.
	 *
	 */
	public String toString() {

		StringBuilder buffer = new StringBuilder();

		buffer.append("id=[").append(id).append("] ");
		buffer.append("quantity=[").append(quantity).append("] ");
		buffer.append("cost=[").append(cost).append("] ");
		buffer.append("createdBy=[").append(createdBy).append("] ");
		buffer.append("createdOn=[").append(createdOn).append("] ");
		buffer.append("modifiedBy=[").append(modifiedBy).append("] ");
		buffer.append("modifiedOn=[").append(modifiedOn).append("] ");

		return buffer.toString();
	}	
	

	/**
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = (int) (prime * result + ((id == null) ? 0 : id.hashCode()));
		return result;
	}

	/**
	 */
	public boolean equals(Object obj) {
		if (obj == this)
			return true;
		if (!(obj instanceof Item))
			return false;
		Item equalCheck = (Item) obj;
		if ((id == null && equalCheck.id != null) || (id != null && equalCheck.id == null))
			return false;
		if (id != null && !id.equals(equalCheck.id))
			return false;
		return true;
	}
}
